<?php
declare (strict_types=1);

namespace app\controller\admin;

use think\Request;
use app\BaseController;

class Manager extends BaseController
{

//    关闭自动实例化模型
//    protected $autoModel=false;


//    protected $modelPath='Manager';

//是否开启自动验证
//    protected $autoValidate = true;

//自定义验证scene场景
//protected $autoValidateScenes=[
//    'save'=>'save1'
//];
//规定排除字段不需要验证
    protected $excludeValidateCheck = ['logout'];


    //测试
    public function kkk()
    {
        return 'asdf';
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $param = request()->param();
        //传入分页与搜索关键词
        $limit = intval(getValByKey('limit', $param, 10));
        $keyword = getValByKey('keyword', $param, '');
        $where = [
            ['username', 'like', '%' . $keyword . '%']
        ];

        $totalCount = $this->M->where($where)->count();
        $list = $this->M->page($param['page'], $limit)
            //关联model/Manager/role
            ->with(['role'])
            ->where($where)
            ->order(['id' => 'desc'])
            ->select()
            ->hidden(['password']);
//        $role = \app\model\Role::field(['id', 'name'])->select();
        return showSuccess([
            'list' => $list,
            'totalCount' => $totalCount,
//            'role'=>$role
        ]);
    }

    /**
     * 创建管理员
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $param = $request->only(['username', 'password', 'avatar', 'role_id', 'status']);
        $res = $this->M->save($param);
        return showSuccess($res);
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $param = $request->only(['id', 'username', 'password', 'avatar', 'role_id', 'status']);
        $res = $request->Model->save($param);
        return showSuccess($res);
    }
    /**
     * 修改登录状态
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function updateStatus()
    {
        $manager=$this->request->Model;
        //不能禁用自己
        if($this->request->UserModel->id===$manager->id){
            ApiException('不能禁用自己');
        }
        $res = $manager->save([
            'status' => $this->request->param('status')
        ]);
        return showSuccess($res);
    }


    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $manager = $this->request->Model;
        //不能删除自己
        if ($this->request->UserModel->id === $manager->id) {
            ApiException('不能删除自己');
        }
        //不能删除管理员账号
        if ($manager->super === 1) {
            ApiException('不能删除超级管理员');
        }
        return showSuccess($manager->delete());
    }


    public function login()
    {
        $user = cms_login([
            'data' => $this->request->UserModel
        ]);
        return showSuccess($user);
    }


    public function logout()
    {
        $res = cms_logout([
            'token' => $this->request->header('token')
        ]);
        return showSuccess($res);
    }

}
